package com.yc.login.dao;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Map;

@ConfigurationProperties(prefix = "myconfig.login")
@Repository
public class UserDao {
    //注入环境参数

    private String userTable;

    private String nameColumn;

    private String pwdColumn;

    private String email;

    @Resource
    private JdbcTemplate jdbcTemplate;

    public Map<String,Object> login(String name, String pwd){
        String sql = "select * from %s where %s=? and %s=?";
        sql = String.format(sql,userTable,nameColumn,pwdColumn);
        try {
            return jdbcTemplate.queryForMap(sql,name,pwd);
        }catch (IncorrectResultSizeDataAccessException e){
            return null;
        }
    }

    public String getUserTable() {
        return userTable;
    }

    public void setUserTable(String userTable) {
        this.userTable = userTable;
    }

    public String getNameColumn() {
        return nameColumn;
    }

    public void setNameColumn(String nameColumn) {
        this.nameColumn = nameColumn;
    }

    public String getPwdColumn() {
        return pwdColumn;
    }

    public void setPwdColumn(String pwdColumn) {
        this.pwdColumn = pwdColumn;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "UserDao{" +
                "userTable='" + userTable + '\'' +
                ", nameColumn='" + nameColumn + '\'' +
                ", pwdColumn='" + pwdColumn + '\'' +
                ", email='" + email + '\'' +
                ", jdbcTemplate=" + jdbcTemplate +
                '}';
    }
}
